package com.app.Activity;

import it.unipr.ce.dsg.s2p.sip.Address;

import com.app.Controller.ActivityController;
import com.app.Controller.NetworkController;




import ReadWriteXML.CMember;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;


public class frmBieuQuyetMoiRoiNhom extends Activity{
	public static Address senderKick;
	public static String beingKickUserName;
	//public static String senderKickUserName;
	public static int timeVoteKick;
	private Thread _bg;
	private boolean isRunning;
	private static int _minutes;
	private int _second;
	public static Runnable doEndTime;
	private AlertDialog.Builder _builder;
	RadioButton radDongY;
	RadioButton radKhongDongY;
	Button btnHoanTat;
	Button btnHuyBo;
	TextView txtTime;
	TextView txtTitle;
	public static Context myContext;
	Runnable dislayKickMemberOut;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.moi_thanh_vien_roi_nhom);
		Log.d("Phu","begin Thoi gian="+timeVoteKick);
		_minutes = timeVoteKick / 60;
		Log.d("Phu","begin phut="+_minutes);
		_second = timeVoteKick - _minutes * 60;
		Log.d("Phu","begin giay="+_second);
		showTimeKick();
		radDongY=(RadioButton)findViewById(R.id.rbtnDongY);
		
		radKhongDongY=(RadioButton)findViewById(R.id.rbtnKhongDongY);
		
		txtTime=(TextView)findViewById(R.id.txtTime);
		txtTitle=(TextView)findViewById(R.id.txtTitle);
		txtTitle.setWidth(500);
		txtTitle.setTextSize(15);
		String title="Đang có yêu cầu mời "+beingKickUserName + " ra khỏi nhóm. Mời bạn biểu quyết";
		txtTitle.setText(title);
//		doEndTime=new Runnable() {
//			
//			@Override
//			public void run() {
//				// TODO Auto-generated method stub
//				doEndTime();
//			}
//		};
		
		_builder=new AlertDialog.Builder(this);
		
		btnHoanTat=(Button)findViewById(R.id.btnHoanTat);
		btnHoanTat.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//BieuQuyet();
				
				int YKienBieuQuyet=YKienBieuQuyet();
				NetworkController.SendFedbackKickMember(senderKick,YKienBieuQuyet);
			
				Log.d("Phu","Da gui fedback kick");
				finish();
			}
		});
		
		btnHuyBo=(Button)findViewById(R.id.btnHuyBo);
		btnHuyBo.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			finish();	
			}
		});
	}
	
	//bỏ
		private void abc(){
		/*
		//=================================================================
//		CountDownTimer timer = new CountDownTimer(5000, 1000) {
//			@Override
//			public void onTick(long millisUntilFinished) {
//				isRunning = true;
//				Thread bg =  new Thread(new Runnable() {
//					@Override
//					public void run() {
//						try {
//							_builder.setMessage("hello")
//							.setCancelable(false)
//							.setPositiveButton("OK", new DialogInterface.OnClickListener() {
//								@Override
//								public void onClick(DialogInterface dialog, int id) {
//									
//									finish();
//								}
//							});
//							AlertDialog alert = _builder.create();
//							alert.getWindow().setBackgroundDrawable(new ColorDrawable(Color.argb(0, 255, 255, 255))); 
//							alert.show();
//						}
//						catch (Throwable t) {
//							Log.d("PEERDROID","Loi Login " + t.getMessage());
//						}
//					}
//				});
//				bg.start();
//			}
//			@Override
//			public void onFinish() {					    	 
//				Toast.makeText(getBaseContext(), "xong phim", Toast.LENGTH_SHORT).show();				    	  					        
//			}
//		}.start();

 */
		//===============================================================
	}
	//
	public int YKienBieuQuyet(){
		int kq=-1;
		if(radDongY.isChecked()){
			kq=1;
		}
		else if(radKhongDongY.isChecked()){
			kq=0;
		}
		else{
			NetworkController.ShowMessageNotice("Bạn chưa chọn ý kiến biểu quyết");
			kq=2;
		}
		return kq;
	}

	private void doEndTime()
	{
		Log.d("Phu","vào do end time");
		
		
	//	_bg.stop();
		
		String ipKicker=NetworkController._peer.getAddressPeer();
		Log.d("Phu","ip kicker="+ipKicker);
		Log.d("Phu","ip sender kick="+senderKick.getURL());
		if(ipKicker.equals(senderKick.getURL())){//la nguoi yeu cau bieu quyet
			//lam sao goi dc ham nay o day
			NetworkController.DislayResultVoteKick();
			finish();
		}
		else{
			Log.d("Phu","vào do end time Stopped");
			
			_builder.setTitle("Thông báo")
			.setMessage("Đã hết thời gian biểu quyết")
			.setCancelable(false)
			.setPositiveButton("OK", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int id) {
					finish();
					
				}
			});
			AlertDialog alert = _builder.create();
			alert.getWindow().setBackgroundDrawable(new ColorDrawable(Color.argb(0, 255, 255, 255))); 
			alert.show();
			//finish();
		}
	}
	
	
	private void showTimeKick(){
		Log.d("Phu","vào show time");
		Log.d("Phu","show time phut= "+_minutes);
		Log.d("Phu","show time giay= "+_second);
		if(txtTime==null){
			txtTime=(TextView)findViewById(R.id.txtTime);
			txtTime.setTextSize(15);
		}
		String s="";
		if(_minutes<10){
			s+="0"+_minutes+":";
		}
		if(_second < 10){
			Log.d("Phu","giay <10");
			s+="0"+_second;
			//txtTime.setText(_minutes + ":0" + _second);
		}
		else{
			Log.d("Phu","giay >10");
			Log.d("Phu","min="+_minutes+" giay="+_second);
			s+=_second;
			//txtTime.setText(_minutes + ":" + _second);
			Log.d("Phu","");
		}
		txtTime.setText(s);
		isRunning = true;
		Log.d("Phu","is running");
		_bg =  new Thread(new Runnable() {
			

			@Override
			public void run() {
				try {
					Log.d("Phu","vào run");
					Log.d("Phu","time" +timeVoteKick );
					Log.d("Phu","_time_min" + _minutes);
					Log.d("Phu","_time sec: " + _second);
					while(timeVoteKick > 0 && isRunning) {
						if(_second == 0)
						{
							_second = 60;
							_minutes--;
						}
						Thread.sleep(1000);
						hander.sendMessage(hander.obtainMessage());
					}
					if(isRunning)
					{	//ActivityController.DoEndTime();
						Log.d("Phu","running");
						//NetworkController.doEndTimeKick();
						ActivityController.hander.post(new Runnable() {
							
							@Override
							public void run() {
								// TODO Auto-generated method stub
							doEndTime();	
							}
						});
						Log.d("Phu","Show do end time");
					}
				}
				catch (Throwable t) {
				}
			}
		});
		_bg.start();
		Log.d("Phu","sau start");
	}
	
	Handler hander=new Handler() {
		@Override
		public void handleMessage(Message msg) {
			Log.d("Phu","Vào hander");
			if(timeVoteKick!=0){
				timeVoteKick --;
				Log.d("Phu","time="+timeVoteKick);
				_second--;
				String tgian="";
				if(_minutes<10){
					
					tgian+="0"+_minutes+":";
				}
				else{
					tgian+=_minutes+":";
				}
				if(_second < 10){
					if(_second<=5){
						txtTime.setTextColor(Color.RED);
						
						txtTime.setTextSize(20);
						
						txtTime.setWidth(100);
					}
					tgian += "0" + _second;
				}
				else{
					tgian+=_second;
				}
				txtTime.setText(tgian);
			}
			else{
				isRunning=false;
			}
		}
	};
	
	
	@Override
	public void onStop()
	{
		super.onStop();
		isRunning=false;
	//	_bg.stop();
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.d("PEERDROID","vao ondestroy");
		isRunning = false;
	//	_bg.stop();
	}
	
	@Override
	public void onStart()
	{
		super.onStart();
		getContext();
	}
	
	
	void getContext(){
		myContext=this.getBaseContext();
	}
		
}
